package com.example.myapplication.ui.consumerInfo;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.NumberPicker;
import android.widget.TimePicker;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.example.myapplication.R;

import org.greenrobot.eventbus.EventBus;

import java.util.Calendar;

public  class BottomDialogFragment extends DialogFragment {
    private String type = "year";
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        setStyle(DialogFragment.STYLE_NORMAL, R.style.BottomDialog);
        View view = inflater.inflate(R.layout.select_year, container);
        initView(view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    protected void initView(View view) {
        SharedPreferences sharedPreferences= getActivity().getSharedPreferences("data", Context.MODE_PRIVATE);
        final SharedPreferences.Editor editor = sharedPreferences.edit();
        final DatePicker datePicker = (DatePicker) view.findViewById(R.id.date_picker);
        //取消按钮
        Button cancel_select_year = (Button) view.findViewById(R.id.cancel_select_year);
        cancel_select_year.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDialog().cancel();
            }
        });
        //确认按钮
        Button confirm_select_year = (Button) view.findViewById(R.id.confirm_select_year);
        confirm_select_year.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDialog().cancel();
                EventBus.getDefault().post(""+datePicker.getYear()+"!"+(datePicker.getMonth()+1)+"!"+type);
            }
        });
        final Button month = (Button) view.findViewById(R.id.month_select_year);
        final Button year = (Button) view.findViewById(R.id.year_select_year);

        //按月
        month.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                type = "month";
                month.setBackgroundResource(R.drawable.button_select);
                year.setBackgroundResource(R.drawable.button_normal);
            }
        });
        //按年

        year.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                type = "year";
                month.setBackgroundResource(R.drawable.button_normal);
                year.setBackgroundResource(R.drawable.button_select);
            }
        });
        //时间滚动
        datePicker.setCalendarViewShown(false);
        //初始化当前日期
        //String[] mDisplayMonths = {"1月", "2月", "3月","4月", "5月", "6月","7月", "8月", "9月","10月", "11月", "12月"};
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), null);

        //只显示年月
        ((ViewGroup) ((ViewGroup) datePicker.getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE);
        //设置月份显示格式
        //((NumberPicker)((ViewGroup) ((ViewGroup) datePicker.getChildAt(0)).getChildAt(0)).getChildAt(0)).setDisplayedValues(mDisplayMonths);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        Dialog dialog = new Dialog(getActivity(), R.style.BottomFragmentDialog);

        // 必须在setContentView之前调用。否则运行时报错。
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(android.R.layout.simple_list_item_2, null);

        // 底部弹出的DialogFragment装载的View
        dialog.setContentView(view);

        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);

        // 设置底部弹出显示的DialogFragment窗口属性。
        Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = 1000; // 底部弹出的DialogFragment的高度，如果是MATCH_PARENT则铺满整个窗口
        window.setAttributes(params);
        return dialog;
    }
}